﻿// The MIT License (MIT)
//
// Copyright (c) 2015 FramTack, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//
// For more information visit the project home page at http://solutionfamily.codeplex.com or the owner's site at http://solution-family.com
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Net;
using System.Diagnostics;

namespace SolutionFamily.IO.Simma
{
    public class J1939Tx
    {
        private byte[] m_data;

        public J1939Tx(int port, J1939TransmitMessage message)
        {
            byte p;

            switch (port)
            {
                case 0:
                    p = 0;
                    break;
                case 1:
                    p = 1;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            var length = message.Data.Length + 8 + 1; // data + header + chksum
            m_data = new byte[length + 3]; // payload + start sentinel + length

            m_data[0] = 192;
            m_data[1] = (byte)(length >> 8); // length MSB
            m_data[2] = (byte)(length & 0xff); // length LSB

            m_data[3] = (byte)MessageIdentifier.TX_J1939; // command
            m_data[4] = p; // port (0 or 1)

            // PGN is 3 bytes, MSB first
            m_data[5] = (byte)((message.PGN & 0x00FF0000) >> 16); // pgn MSB
            m_data[6] = (byte)((message.PGN & 0x0000FF00) >> 8);
            m_data[7] = (byte)(message.PGN & 0x000000FF); // pgn LSB

            m_data[8] = (byte)message.DestinationAddress; // destination
            m_data[9] = (byte)message.SourceAddress; // source
            m_data[10] = (byte)message.Priority; // priority

            Buffer.BlockCopy(message.Data, 0, m_data, 11, message.Data.Length);

            var e = VNA232.TwosComplement(0, m_data, 1, m_data.Length - 2);
            m_data[m_data.Length - 1] = (byte)(256 - e);
        }

        public byte[] Serialize()
        {
            return m_data;
        }
    }

    public class J1939Px
    {
        private byte[] m_data;

        public J1939Px(int port, J1939TransmitMessage message)
        {
            byte p;

            switch (port)
            {
                case 0:
                    p = 0;
                    break;
                case 1:
                    p = 1;
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            var length = message.Data.Length + 8 + 2 + 1; // data + header + transmit period + chksum
            m_data = new byte[length + 3]; // payload + start sentinel + length

            m_data[0] = 192;
            m_data[1] = (byte)(length >> 8); // length MSB
            m_data[2] = (byte)(length & 0xff); // length LSB

            m_data[3] = (byte)MessageIdentifier.PX_J1939; // command
            m_data[4] = p; // port (0 or 1)

            // PGN is 3 bytes, MSB first
            m_data[5] = (byte)((message.PGN & 0x00FF0000) >> 16); // pgn MSB
            m_data[6] = (byte)((message.PGN & 0x0000FF00) >> 8);
            m_data[7] = (byte)(message.PGN & 0x000000FF); // pgn LSB

            m_data[8] = (byte)message.DestinationAddress; // destination
            m_data[9] = (byte)message.SourceAddress; // source
            m_data[10] = (byte)message.Priority; // priority

            Buffer.BlockCopy(message.Data, 0, m_data, 11, message.Data.Length);

            // period follows data
            var period = 500;
            m_data[m_data.Length - 3] = (byte)(period >> 8);
            m_data[m_data.Length - 2] = (byte)(period & 0xff);

            var e = VNA232.TwosComplement(0, m_data, 1, m_data.Length - 2);
            m_data[m_data.Length - 1] = (byte)(256 - e);
        }

        public byte[] Serialize()
        {
            return m_data;
        }
    }
}
